package com.example.checkbox;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.Toast;

public class checkbox extends AppCompatActivity {

    LinearLayout container;
    Button btn_login;
    Button btn_exit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);

        container = (LinearLayout)findViewById(R.id.container);
        btn_login = (Button)findViewById(R.id.confirm_btn);
        btn_exit = (Button)findViewById(R.id.cancel_btn);



        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String checked = "";
                for (int i = 0;i<container.getChildCount();i++){
                    View temp_ctrl = container.getChildAt(i);
                    if (temp_ctrl instanceof CheckBox)
                    {
                        CheckBox temp_check = (CheckBox)temp_ctrl;
                        if (temp_check.isChecked()){
                            checked += temp_check.getText().toString();

                        }
                    }
                }
                Toast.makeText(checkbox.this, checked, Toast.LENGTH_LONG).show();

            }
        });



        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(checkbox.this);
                builder.setMessage(R.string.tip_exit);
                builder.setNegativeButton(R.string.btn_exit_yes,new  DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog,int i) {
                        System.exit(0);
                    }
                });
                builder.setPositiveButton(R.string.per_cancel,null);
                builder.create().show();
            }
        });





    }



}